let args = {}
window.location.search.substring(1).split("&").forEach(function (arg) {
	let pair = arg.split("=")
	if (pair.length && pair[0]) {
		if (pair.length == 1)
			args[pair[0]] = true
		else
			args[pair[0]] = pair[1]
	}
})

let seed = 16
let engine = marbleRaceEngineMatter0190
let uiEngine = marbleRaceUIEngine
let config = marbleRaceColor50
if (args.config) {
	marbleRaceColors.forEach(function (colors) {
		if (colors.id == args.config)
			config = colors.colors
	})
}
if (args.elastic) {
	let elastic = parseFloat(args.elastic)
	if (elastic >= 0)
		engine.elastic = elastic
}
let levels = [
	marbleRaceLevel3_6,
	marbleRaceLevel1_10,
	marbleRaceLevel3_4,
	marbleRaceLevel1_17,
	marbleRaceLevel2_1,

	marbleRaceLevel1_16,
	marbleRaceLevel3_10,
	marbleRaceLevel1_7,
	marbleRaceLevel1_6,
	marbleRaceLevel3_9,

	marbleRaceLevel1_14,
	marbleRaceLevel3_5,
	marbleRaceLevel1_13,
	marbleRaceLevel1_11,
	marbleRaceLevel3_8,

	marbleRaceLevel1_9,
	marbleRaceLevel3_7,
	marbleRaceLevel1_8,
	marbleRaceLevel1_18,
	marbleRaceLevel3_1,

	marbleRaceLevel1_19,
	marbleRaceLevel1_3,
	marbleRaceLevel1_2,
	marbleRaceLevel3_2,
	marbleRaceLevel1_12,

	marbleRaceLevel1_1,
	marbleRaceLevel1_15,
	marbleRaceLevel1_5,
]
let currentLevel = levels.length

engine.seed(seed)

let n = config.colors.length
let m = config.teams.length

let breakNumber = [n / 2]
let breakCount = 0

let stat = new Array(n)
for (let i = 0; i < n; ++i)
	stat[i] = {
		status: marbleRaceConfig.constant.UNKNOWN,
		score: 0
	}
stat.totalRank = n

uiEngine.init(marbleRaceConfig.text.startGamePrompt, config, stat)

let levelCurrent = 0

function runLevel() {
	let isFinalLevel = false
	let currentMarbles = 0
	let gameLevel
	for (let i = 0; i < n; ++i)
		if (stat[i].status != marbleRaceConfig.constant.ELIMINATED)
			++currentMarbles;
	
	if (currentMarbles == 1) {
		isFinalLevel = true
		gameLevel = marbleRaceLevelC
	} else {
		while (breakNumber.length && currentMarbles <= breakNumber[breakNumber.length - 1]) {
			breakNumber.pop()
			++breakCount
		}
		
		if (breakCount) {
			--breakCount
			gameLevel = marbleRaceLevelR
		} else {
			if (currentMarbles == 2) {
				gameLevel = marbleRaceLevel2_49
			} else {
				if (currentLevel == levels.length)
					currentLevel = 0
				gameLevel = levels[currentLevel++]
			}
		}
	}
	let isLevel = marbleRaceConfig.isLevel[gameLevel.type]
	let levelText = ''
	if (isLevel)
		levelText = marbleRaceConfig.text.level.replace('%l', (++levelCurrent).toString())
	else
		levelText = gameLevel.title
	uiEngine.init(levelText, config, stat, { calcTime: isLevel })

	let onEnd
	if (isFinalLevel)
		onEnd = function () { }
	else
		onEnd = function () {
			setTimeout(function () {
				uiEngine.del()
				engine.del()
				runLevel()
			}, marbleRaceConfig.nextLevelDelay)
		}

	engine.start(gameLevel, config, stat, onEnd)
}

let beforeGameStarted = true
function startGame() {
	if (beforeGameStarted) {
		beforeGameStarted = false
		uiEngine.del()
		runLevel()
	}
}
document.addEventListener('keydown', function (event) {
	if (event.code == marbleRaceConfig.startGameKeyCode)
		startGame()
})
document.addEventListener('dblclick', function (event) {
	startGame()
})
document.addEventListener('touchend', function (event) {
	startGame()
})